import {createRouter, RouteRecordRaw, createWebHashHistory} from "vue-router";
import userRoutes from "~/router/modules/user";
import {getJWTToken} from "~/request/utils";

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        redirect: "/main/home",
    },
    {
        name: "main",
        path: "/main",
        component: () => import("~/layout/index.vue"),
        children: [
            {
                path: "home",
                component: () => import("~/application/home/index.vue"),
                name: "home"
            },
            ...userRoutes,
        ]
    }
]
const whiteList = ["/main/home"]
const router = createRouter({
    history: createWebHashHistory(),
    routes: routes,
})
// 全局路由守卫！
router.beforeEach((to, from) => {
    if (whiteList.indexOf(to.path) > -1) {
        return true
    }
    if (getJWTToken()) {
        return true
    }
    // 返回 false 以取消导航
    router.push("/main/home").then(() => {
        window.NMessage.warning("请先登录哈！")
        return true
    })
})
export default router;
